package com.example.demo.service.impl;

import com.example.demo.domain.Catalog;
import com.example.demo.mapper.CatalogMapper;
import com.ffcs.oss.param.out.ServiceResp;
import com.example.demo.repository.CatalogRepository;
import com.example.demo.security.SecurityUtils;
import com.example.demo.service.BaseService;
import com.example.demo.service.CatalogService;
import com.example.demo.web.rest.evt.ArticleCatalogIdEvt;
import com.example.demo.web.rest.evt.IdEvt;
import com.example.demo.web.rest.evt.RelationEvt;
import com.example.demo.web.rest.evt.SaveCatalogEvt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author zhengmf
 * @Date 2019/1/8  15:12
 */
@Service
@Transactional
public class CatalogServiceImpl extends BaseService implements CatalogService {
    @Autowired
    private CatalogRepository catalogRepository;
    @Autowired
    private CatalogMapper catalogMapper;

    @Override
    public void saveCatalog(SaveCatalogEvt evt) {
        Catalog catalog = new Catalog();
        BeanUtils.copyProperties(evt,catalog);
        Date date = new Date();
        if(catalog.getCatalogId() == null){
            catalog.setCreateUser(SecurityUtils.getCurrentUserLogin().orElse(""));
            catalog.setCreateTime(date);
        }
        catalog.setModifyUser(SecurityUtils.getCurrentUserLogin().orElse(""));
        catalog.setModifyTime(date);
        Catalog newCatalog = catalogRepository.save(catalog);
        Long catalogId = newCatalog.getCatalogId();
        String fullCatalogId;
        if(catalog.getParentId() != null){
            IdEvt idEvt = new IdEvt();
            idEvt.setId(catalog.getParentId());
            String parentFullCatalogId = baseQueryClient.getFullCatalogIdById(idEvt).getBody();
            fullCatalogId = parentFullCatalogId+catalogId+",";

        }else {
            fullCatalogId = ","+catalogId+",";
        }
        catalogRepository.setFullCatalogId(fullCatalogId,catalogId);
    }

    @Override
    public ServiceResp<Boolean> delCatalog(Long catalogId) {
        IdEvt idEvt = new IdEvt();
        idEvt.setId(catalogId);
        if(baseQueryClient.hasChild(idEvt).getBody()){
            return ServiceResp.getInstance(Boolean.class).success(false,"请先删除子目录！");
        }
        catalogRepository.deleteById(catalogId);
        //删除关系
        catalogMapper.delArticleCatalogByCatalogId(catalogId);
        return ServiceResp.getInstance(Boolean.class).success(true,"删除成功！");
    }

    @Override
    public void addArticleCatalog(RelationEvt evt) {
        Integer addCount = catalogMapper.addArticleCatalog(evt);
        logger.info("插入文章目录关系" + addCount + "条！");
    }

    @Override
    public void delArticleCatalog(ArticleCatalogIdEvt evt) {
        Integer count = catalogMapper.delArticleCatalog(evt);
        logger.info("插入文章目录关系" + count + "条！");
    }
}
